package com.like.fragment;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;

import com.bokecc.sdk.mobile.play.DWMediaPlayer;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.like.adapter.ArticleReplyAdapter;
import com.like.entity.GeneralResponse;
import com.like.entity.InfoGetReplyReq;
import com.like.entity.InfoGetReplyResp;
import com.like.entity.MeetingDetailResp;
import com.like.hospitalworld.R;
import com.like.likeutils.network.RetrofitCallback;
import com.like.network.InfoService;
import com.like.network.MeetingService;
import com.like.network.RetrofitUtil;
import com.like.utils.Constants;
import com.like.widget.MyToast;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.util.List;

import retrofit2.Call;
import retrofit2.Response;

@ContentView(R.layout.fragment_video_content)
public class VideoContentFragment extends BaseFragment implements SurfaceHolder.Callback, MediaPlayer.OnPreparedListener, MediaPlayer.OnErrorListener {

    @ViewInject(R.id.reply_list)
    PullToRefreshListView mReplyList;
    @ViewInject(R.id.video_container)
    SurfaceView mSurface;
    @ViewInject(R.id.play_icon)
    ImageView mPlayIcon;

    private ArticleReplyAdapter mReplyAdapter;
    private MeetingService mMeetingService;
    private InfoService mInfoService;
    private SurfaceHolder mSurfaceHolder;
    private DWMediaPlayer mPlayer;
    //    private MeetingCourseListResp.MeetingCourse mMeetingCourse;
    private MeetingDetailResp mMeetingDetail;
    private int mMeetingId = 0;
    private boolean mVideoPrepared = false;
    private int mCurrentPage = 0;
    private int mPageCount = 15;

    @Override
    protected void onCreateView() {
        super.onCreateView();
        mMeetingService = RetrofitUtil.getService(MeetingService.class);
        mInfoService = RetrofitUtil.getService(InfoService.class);
        initList();
        initSurface();
    }

    @Override
    public void onStop() {
        super.onStop();
//        if (mPlayer != null && mPlayer.isPlaying()) {
//            videoClick(null);
//        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        release();
    }

    private void release() {
        if (mPlayer != null) {
            mPlayer.reset();
            mPlayer = null;
            mVideoPrepared = false;
        }
    }

    public void setMeetingDetail(MeetingDetailResp detail) {
        this.mMeetingDetail = detail;
        getReplyList(false);
        mPlayer.setVideoPlayInfo(mMeetingDetail.redId, Constants.VIDEO_USER_ID, Constants.VIDEO_KEY, m.mContext);
        mPlayer.prepareAsync();
//        prepareVideo(course);
    }

    private void initSurface() {
        mSurfaceHolder = mSurface.getHolder();
        mSurfaceHolder.addCallback(this);
    }

    private void initList() {
        mReplyList.setMode(PullToRefreshBase.Mode.BOTH);
        mReplyList.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                mCurrentPage = 0;
                getReplyList(false);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                mCurrentPage += 1;
                getReplyList(true);
            }
        });
    }

    @Event({R.id.video_container, R.id.play_icon})
    private void videoClick(View v) {
        if (mPlayer == null || !mVideoPrepared) {
            MyToast.showBlueCircleToast(m.mContext, "视屏正在加载中");
            return;
        }
        if (mPlayer.isPlaying()) {
            mPlayer.pause();
            mPlayIcon.setVisibility(View.VISIBLE);
        } else {
            mPlayer.start();
            mPlayIcon.setVisibility(View.GONE);
        }
    }

    public void setMeetingId(int meetingId) {
        mMeetingId = meetingId;
    }

    public void refreshReplyList() {
        mCurrentPage = 0;
        getReplyList(false);
    }

    private void getReplyList(final boolean append) {
        if (m == null)
            return;
        InfoGetReplyReq req = new InfoGetReplyReq(mMeetingDetail.id, mCurrentPage, mPageCount);
        Call<GeneralResponse<InfoGetReplyResp>> call = mInfoService.getInfoReplyList(Constants.getGUID(), m.getToken(), req);
        call.enqueue(new RetrofitCallback<GeneralResponse<InfoGetReplyResp>>() {
            @Override
            public void onResponse(Call<GeneralResponse<InfoGetReplyResp>> call, Response<GeneralResponse<InfoGetReplyResp>> response) {
                mReplyList.onRefreshComplete();
                InfoGetReplyResp resp = response.body().data;
                List<InfoGetReplyResp.InformationComment> comments = resp.comments;
                if (mReplyAdapter == null) {
                    mReplyAdapter = new ArticleReplyAdapter(comments, m);
                    mReplyList.setAdapter(mReplyAdapter);
                } else
                    mReplyAdapter.update(comments, append);
            }

            @Override
            public void onFailure(Call<GeneralResponse<InfoGetReplyResp>> call, Throwable t) {
                t.printStackTrace();
            }
        });
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        mPlayer = new DWMediaPlayer();
        mPlayer.setDisplay(mSurfaceHolder);
        mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mPlayer.setOnPreparedListener(this);
        mPlayer.setOnErrorListener(this);
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
    }

    @Override
    public void onPrepared(MediaPlayer mp) {
        mVideoPrepared = true;
        MyToast.showBlueCircleToast(m.mContext, "视频加载完成");
        int videoHeight = mPlayer.getVideoHeight();
        int videoWidth = mPlayer.getVideoWidth();
        System.out.println(videoHeight + "   " + videoWidth);
    }

    @Override
    public boolean onError(MediaPlayer mp, int what, int extra) {
        System.out.println("初始化失败" + what + "   " + extra);
        mPlayer.setVideoPlayInfo(mMeetingDetail.redId, Constants.VIDEO_USER_ID_2, Constants.VIDEO_KEY_2, m.mContext);
        mPlayer.prepareAsync();
        return false;
    }
}
